<?php

namespace App\Model;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\SaAdmin.
 *
 * @property int         $id
 * @property string      $username   名称
 * @property string      $password   密码
 * @property string      $safe       安全码
 * @property int         $role_id    角色id
 * @property int         $status     状态，是否禁用 : 0否，1是
 * @property int         $login_num  登录次数
 * @property int|null    $last_login 最后登录时间
 * @property string|null $last_ip    最后登录ip
 * @property int|null    $creator_id 创建人id
 * @property string|null $creator    创建人
 * @property int|null    $updater_id 更新人id
 * @property string|null $updater    更新人
 * @property Carbon|null $created_at 创建时间
 * @property Carbon|null $updated_at 更新时间
 * @property Carbon|null $deleted_at 删除时间
 *
 * @method static Builder|SaAdmin newModelQuery()
 * @method static Builder|SaAdmin newQuery()
 * @method static \Illuminate\Database\Query\Builder|SaAdmin onlyTrashed()
 * @method static Builder|SaAdmin query()
 * @method static Builder|SaAdmin whereCreatedAt($value)
 * @method static Builder|SaAdmin whereCreator($value)
 * @method static Builder|SaAdmin whereCreatorId($value)
 * @method static Builder|SaAdmin whereDeletedAt($value)
 * @method static Builder|SaAdmin whereId($value)
 * @method static Builder|SaAdmin whereLastIp($value)
 * @method static Builder|SaAdmin whereLastLogin($value)
 * @method static Builder|SaAdmin whereLoginNum($value)
 * @method static Builder|SaAdmin wherePassword($value)
 * @method static Builder|SaAdmin whereRoleId($value)
 * @method static Builder|SaAdmin whereSafe($value)
 * @method static Builder|SaAdmin whereStatus($value)
 * @method static Builder|SaAdmin whereUpdatedAt($value)
 * @method static Builder|SaAdmin whereUpdater($value)
 * @method static Builder|SaAdmin whereUpdaterId($value)
 * @method static Builder|SaAdmin whereUsername($value)
 * @method static \Illuminate\Database\Query\Builder|SaAdmin withTrashed()
 * @method static \Illuminate\Database\Query\Builder|SaAdmin withoutTrashed()
 * @mixin \Eloquent
 */
class SaAdmin extends BaseModel
{
    use SoftDeletes;
    protected $table = 'sa_admin';

    /**
     * 格式化列表数据.
     *
     * @param $list
     */
    public static function formatList(&$list)
    {
        if (empty($list)) {
            return;
        }

        $roleIdxs        = SaIndexRoleAdmin::query()->where('admin_id', array_column($list, 'id'))->get(['role_id', 'admin_id']);
        $roleList        = SaRole::query()->whereIn('id', $roleIdxs->pluck('role_id'))->get(['id', 'name', 'status', 'is_admin']);

        foreach ($list as &$item) {
            $item['role_id']    = $roleIdxs->where('admin_id', $item['id'])->pluck('role_id')->unique()->implode(',');
            $item['role_name']  = $roleList->whereIn('id', $roleIdxs->where('admin_id', $item['id'])->pluck('role_id'))->pluck('name')->implode(',');
            $item['last_login'] = $item['last_login'] ? date_time_str($item['last_login']) : '';
        }
    }

    /**
     * 获取管理员信息.
     *
     * @param $param
     *
     * @return array
     */
    public static function getInfo($param)
    {
        $where = [];
        if (array_key_exists('id', $param) && !empty($param['id'])) {
            $where[] = ['id', '=', $param['id']];
        }
        if (array_key_exists('username', $param) && !empty($param['username'])) {
            $where[] = ['username', '=', $param['username']];
        }
        $info     = self::query()->where($where)->first();
        if (!$info) {
            return [];
        }

        $roleIds           = SaIndexRoleAdmin::query()->where('admin_id', $info['id'])->get(['role_id'])->pluck('role_id')->toArray();
        $roleList          = SaRole::query()->whereIn('id', $roleIds)->get(['id', 'name', 'status', 'is_admin']);
        $info->role_name   = $roleList->pluck('name')->implode(',');
        $info->role_status = $roleList->where('status', 1)->count()   >= 1 ? 1 : 0;
        $info->is_admin    = $roleList->where('is_admin', 1)->count() >= 1 ? 1 : 0;

        return $info->toArray();
    }

    /**
     * 获取管理员人数.
     *
     * @param $param
     *
     * @return mixed
     */
    public static function getCount($param = [])
    {
        $where = [];
        if (array_key_exists('role_id', $param) && !empty($param['role_id'])) {
            $where[] = ['role_id', '=', $param['role_id']];
        }

        return self::where($where)->count();
    }

    /**
     * 添加管理员.
     *
     * @param $info
     *
     * @return mixed
     */
    public static function addAdmin($info)
    {
        return self::insertGetId($info);
    }

    /**
     * 编辑管理员.
     *
     * @param $id
     * @param $info
     *
     * @return mixed
     */
    public static function editAdmin($id, $info)
    {
        return self::where(['id' => $id])->update($info);
    }

    /**
     * 删除管理员.
     *
     * @param $id
     *
     * @return mixed
     */
    public static function delAdmin($id)
    {
        return self::where(['id' => $id])->delete();
    }
}
